package com.beyondsoft.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.beyondsoft.R;
import com.beyondsoft.dao.BookDao;
import com.beyondsoft.dao.DBManager;
import com.beyondsoft.model.Book;

public class SqLiteProjectActivity extends Activity {
	private EditText name;
	private EditText author;
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	private Map<String, String> map = null;
	private ListView listView;
	private LinearLayout layout1;
	private LinearLayout layout2;
	private Button btnSave;
	private Button btnInsert;
	private Button btnCancel;
	private Button btnFind;
	private Button btnUp;
	static String _id;
	private BookDao dao = new BookDao(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		name = (EditText) this.findViewById(R.id.etName);
		author = (EditText) this.findViewById(R.id.etAuthor);
		listView = (ListView) this.findViewById(R.id.bookList);
		hideView(false);
		refreshBooks();
	}

	/**
	 * @param view
	 */
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btnInsert) {
			hideView(true);
		}
		if (id == R.id.btnCancel) {
			hideView(false);
		}
		if (id == R.id.btnSave) {
			saveBook();
		}
		if (id == R.id.btnUp) {
			updateBook();
		}
		if (id == R.id.btnFind) {
			refreshBooks();
		}
	}

	private void saveBook() {
		Book book = new Book();
		book.setBook_name(name.getText().toString());
		book.setAuthor(author.getText().toString());
		dao.insert(book);
		UIUtil.showToast(this, "添加书籍成功");
		hideView(false);
		refreshBooks();
	}
	
	private void updateBook() {
		Book book = new Book();
		book.setBook_name(name.getText().toString());
		book.setAuthor(author.getText().toString());
		book.setId(Integer.parseInt(_id));
		dao.update(book);
		UIUtil.showToast(this, "更新成功");
		hideView(false);
		refreshBooks();
	}

	private void refreshBooks() {
		list.clear();
		List<Book> books = dao.findAll();
		if (books == null) {
			return;
		}
		for (Book book : books) {
			Log.i("sys", book.toString() + "");
			map = new HashMap<String, String>();
			int book_id = book.getId();
			String book_name = book.getBook_name();
			String book_auhor = book.getAuthor();
			if (book_auhor.length() > 5) {
				book_auhor = book_auhor.substring(0, 4) + "..";
			}
			map.put("id", String.valueOf(book_id));
			map.put("name", book_name);
			map.put("author", book_auhor);
			list.add(map);
		}
		showList();
	}

	private void showList() {
		SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item,
				new String[] { "id", "name", "author" }, new int[] {
						R.id.bookId, R.id.bookName, R.id.bookAuthor });
		listView.setAdapter(adapter);
		// 单击监听，操作
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				Map<String, String> appmap = list.get(position);
				String id = (String) appmap.get("id");//
				String name = (String) appmap.get("name");
				showDialog(id, name);// 显示对话框
			}
		});
		// 长按监听,编辑
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, String> appmap = list.get(position);
				_id = (String) appmap.get("id");
				String _name = (String) appmap.get("name");
				String _author = (String) appmap.get("author");
				name.setText(_name);
				author.setText(_author);
				isEdit();
				return true;
			}
		});
	}

	public void showDialog(final String id, String name) {
		Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.book);
		builder.setTitle("操作");
		builder.setMessage("" + name);
		builder.setPositiveButton("购买", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				UIUtil.showToast(SqLiteProjectActivity.this, "购买成功");
			}
		});
		builder.setNeutralButton("删除", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				boolean flag = dao.delete(id);
				if (flag) {
					UIUtil.showToast(SqLiteProjectActivity.this, "删除成功");
					refreshBooks();
				} else
					UIUtil.showToast(SqLiteProjectActivity.this, "删除失败");
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// TODO
			}
		});
		builder.show();
	}

	public void hideView(boolean flag) {
		layout1 = (LinearLayout) this.findViewById(R.id.editLayout1);
		layout2 = (LinearLayout) this.findViewById(R.id.editLayout2);
		btnInsert = (Button) this.findViewById(R.id.btnInsert);
		btnSave = (Button) this.findViewById(R.id.btnSave);
		btnFind = (Button) this.findViewById(R.id.btnFind);
		btnCancel = (Button) this.findViewById(R.id.btnCancel);
		btnUp = (Button) this.findViewById(R.id.btnUp);
		btnUp.setVisibility(View.GONE);
		if (flag) {
			layout1.setVisibility(View.VISIBLE);
			layout2.setVisibility(View.VISIBLE);
			btnSave.setVisibility(View.VISIBLE);
			btnInsert.setVisibility(View.GONE);
			btnCancel.setVisibility(View.VISIBLE);
			btnFind.setVisibility(View.GONE);
		} else {
			layout1.setVisibility(View.GONE);
			layout2.setVisibility(View.GONE);
			btnSave.setVisibility(View.GONE);
			btnInsert.setVisibility(View.VISIBLE);
			btnCancel.setVisibility(View.GONE);
			btnFind.setVisibility(View.VISIBLE);
		}
	}

	public void isEdit() {
		hideView(true);
		btnSave.setVisibility(View.GONE);
		btnUp.setVisibility(View.VISIBLE);
	}
}